package com.two.service.permission.controller;

import com.two.common.util.CommonResult;
import com.two.service.permission.feign.UserFeign;
import com.two.service.permission.service.IPermissionService;
import com.two.service.permission.service.IRolePermissionService;
import com.two.service.permission.service.impl.PermissionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 汪瑞明
 * @since 2022-08-29
 */
@RestController
@RequestMapping("permission/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IRolePermissionService rolePermissionService;

    @Autowired
    private UserFeign userFeign;

    // 查询左侧菜单
    @PostMapping("/leftMenu")
    public CommonResult leftMenu(HttpServletRequest request){
        String token = request.getHeader("token");
        return permissionService.getLeftMenu(token);
    }

    // 退出
    @PostMapping ("/logout")
    public CommonResult logout(HttpServletRequest request){
        String token = request.getHeader("token");
        if(redisTemplate.hasKey(token)){
            redisTemplate.delete(token);
            return new CommonResult(2000,"退出成功",null);
        }
        return new CommonResult(5000,"无效得token",null);
    }

    // 个人信息
    @PostMapping("/personal")
    public CommonResult personal(String token){
        return userFeign.selectById(token);
    }
    // 点击修改权限按钮事件
    @PostMapping("/menu")
    public CommonResult menu(Integer id){
        return permissionService.selectPermissionByRoleId(id);
    }

    // 确认修改权限
    @PostMapping("/yes")
    public CommonResult yes(Integer roleid, @RequestBody List<String> ids){
        return rolePermissionService.yes(roleid,ids);
    }
}
